package com.zat.mes.andrmes.dofun.work

import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.dofun.work.fragment.DayRecordFragment
import com.zat.mes.andrmes.dofun.work.vm.WorkRecordViewModel
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.DateUtil
import com.zat.mes.andrmes.utils.PreferencesUtil
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.layout_title.*
import org.json.JSONObject
import java.util.*

/**
 * 做工记录列表activity
 */
class DayWorkRecordListActivity : BaseActivity() {
    companion object {
        val TYPE_NOMAL = 0
        val TYPE_QR = 1
    }

    private var empName: String = ""
    var day: String = ""
    private var empId: Long = 0
    override fun init() {
        initView()
        initData()
    }

    /**
     * 初始化数据
     */
    private fun initData() {
        if (intent.getIntExtra(CompanionType.INTENT_EXTRA_TYPE, 0) == TYPE_NOMAL) {
            day = intent.getStringExtra(CompanionType.INTENT_EXTRA_DAY)
            empId = intent.getLongExtra(CompanionType.INTENT_EXTRA_EMPID, 0)
            empName = intent.getStringExtra(CompanionType.INTENT_EXTRA_EMPNAME)
            initFragment()
        } else if (intent.getIntExtra(CompanionType.INTENT_EXTRA_TYPE, 0) == TYPE_QR) {
            //二维码进入
            day = DateUtil.formatDate(Date(), DateUtil.FORMAT_3)
            val qrCode = intent.getStringExtra(CompanionType.INTENT_EXTRA_QRCODE)
            val loginResultBean = Gson().fromJson(
                PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
                LoginResultBean::class.java
            )
            empId = loginResultBean.empid
            empName = loginResultBean.empname
            val recordViewModel = ViewModelProvider(this).get(WorkRecordViewModel::class.java)
            recordViewModel.getPackData().observe(this, androidx.lifecycle.Observer {
                if (it == null) {
                    showToast(getString(R.string.text_not_find_pack))
                } else {

                    val jsonObject = JSONObject()
                    jsonObject.put("empId", empId);
                    jsonObject.put("empName", empName);
                    jsonObject.put("packId", it.packId);
                    jsonObject.put("packNo", it.packNO);
                    jsonObject.put("orderId", it.orderId);
                    jsonObject.put("styleNO", it.styleNO);
                    jsonObject.put("productName", it.productName);
                    jsonObject.put("color", it.color);
                    jsonObject.put("size", it.size);
                    jsonObject.put("quantity", it.quantity)
                    recordViewModel.loadRecordData(jsonObject.toString())

                }

            })
            recordViewModel.getRecordData().observe(this, androidx.lifecycle.Observer {
                //                showToast(it)
                initFragment()
            })
            recordViewModel.loadPackData(qrCode)
        }
        tVtitle.text = empName
    }

    /**
     * 初始化fragment
     */
    private fun initFragment() {
        val beginTransaction = supportFragmentManager.beginTransaction()
        beginTransaction.add(R.id.flContentDp, DayRecordFragment.newInstance(empId)).commit()
    }

    /**
     * 初始化view
     */
    private fun initView() {
        setContentView(R.layout.activity_day_produce_plan_list)

        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
    }


}
